<?php
require_once 'include/inc.config.php';
require_once WACT_ROOT . '/db/db.inc.php';
require_once APP_ROOT.'model/common/utils.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/date.php';
require_once APP_ROOT.'model/common/parameters.php';
require_once APP_ROOT.'model/common/site.php';



class StatLeague {


    function insertLeague(&$data) {
    	$site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);
    	$enabled = $data->get('enabled');
    	if (!$enabled) $data->set('enabled',0);

    	$tz =& new MyTimeZone();
        $data->set('date_added', gmdate("Y-m-d H:i", $tz->timeNowLocal() ) );

        $Record =& DBC::NewRecord($data);
        return $Record->insertId('sport_stat_leagues',
            array('site_id','leagueName', 'enabled', 'date_added'), 'leagueID');
    }



    function &getRecord($id) {
        return DBC::FindRecord("SELECT L.*
        FROM sport_stat_leagues L
        WHERE L.leagueID = ". DBC::makeLiteral($id));
    }

    function updateLeague($id, &$data) {
        $site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);

    	$enabled = $data->get('enabled');
    	if (!$enabled) $data->set('enabled',0);

        $Record =& DBC::NewRecord($data);
        $Record->update('sport_stat_leagues',
            array('site_id','leagueName', 'enabled'),
            "leagueID=". DBC::makeLiteral($id));
    }

    function denyLeague($league_id) {
        $data = new DataSpace();
        $data->set('enabled',0);

    	$Record =& DBC::NewRecord($data);
        $Record->update('sport_stat_leagues', array('enabled'),
            "leagueID=". DBC::makeLiteral($league_id));
    }

    function publishLeague($league_id) {
        $data = new DataSpace();
        $data->set('enabled',1);

    	$Record =& DBC::NewRecord($data);
        $Record->update('sport_stat_leagues',
            array('enabled'), "leagueID=". DBC::makeLiteral($league_id));
    }



	function delete($id) {
        DBC::execute("DELETE FROM sport_stat_leagues WHERE leagueID=". DBC::makeLiteral($id));
    }

    function &getLeaguesList(&$Pager, &$request, &$responseModel,$options=null, $onlynew=false) {
        $site_id = Site::getSiteID();
     	//restore filter from session
     	Session::loadResponseModel($responseModel,'filter.statleagues');
     	$sql = "select L.*
     		from sport_stat_leagues L
     		";

     	if($options==null){
     		if ($onlynew) {
                $sql.=" where 1=1 and (L.enabled=0 or L.enabled is null)
                and L.site_id = ".DBC::makeLiteral($site_id)." ".StatLeague::getLeaguesFilter($request, $responseModel)."
     		    order by L.leagueName, L.date_added desc  ";
     		} else {
     			$sql.=" where 1=1
                and L.site_id = ".DBC::makeLiteral($site_id)." ".StatLeague::getLeaguesFilter($request, $responseModel)."
     		 	order by ".StatLeague::getLeaguesOrder($request, $responseModel);
     		}
            //print $sql;
        	return DBC::NewPagedRecordSet($sql, $Pager);
     	}else{
     		$sql.=" where 1=1
             and L.site_id = ".DBC::makeLiteral($site_id);
     		$sql.=" order by L.leagueName, L.date_added desc ";
     		error_log("[SQL:]$sql");
     		//print $sql;
     		return DBC::NewPagedRecordSet($sql, $Pager);
     	}
     }



    //get the array of leagues for list
	function &getLeaguesForList() {
		$site_id = Site::getSiteID();
		$sql = "select L.*    from sport_stat_leagues L ";
        $sql .= " where L.site_id =  ".DBC::makeLiteral($site_id);
        $sql.=" order by L.leagueName, L.date_added desc ";
		$result =& DBC::NewRecordSet($sql);
		$result->reset();
		//$i=1;
        $leagues=array();

        while($result->next()) {
        		$id =  $result->get('leagueID');
        		$name = $result->get('leagueName');

                $leagues[$id]= $name;
        }
        return $leagues;
	}

     function getLeaguesFilter(&$request,&$responseModel) {
      	$where = '';
      	$filter_league_id = $responseModel->get('filter_league_id');
        $league_id = $responseModel->get('league_id');
      	if ($filter_league_id)  $where .= ' and L.leagueID = '.DBC::makeLiteral($league_id);

        $filter_title = $responseModel->get('filter_title');
        $title = $responseModel->get('title');
        if ($filter_title)  $where .= ' and L.leagueName like '.DBC::makeLiteral('%'.$title.'%').'  ';

        return $where;
      }

      function getLeaguesOrder(&$request,&$responseModel) {
      	$sortby = 'L.leagueName';
      	$sortby = $responseModel->get('sort');
      	if ($sortby) return 'L.'.$sortby;
      	else return 'L.leagueName';
      }


      function &getNewLeaguesList(&$Pager, &$request, &$responseModel) {
      	return StatLeague::getLeaguesList($Pager, $request, $responseModel,null,true);
      }



     function &getLeaguesPublic($currentLeagueID) {
        $site_id = Site::getSiteID();

     	$sql = "select L.* from sport_stat_leagues L where L.enabled=1 ";
     	$sql.=" and L.site_id = ".DBC::makeLiteral($site_id);
        $sql.=" order by L.leagueName ";
     	//print  $sql;
        $rs =& DBC::NewRecordSet($sql);

        $list_updated=array();

        $rs->reset();
        while ($rs->next()) {

        	$dataspace = $rs->export();

            $leagueID = $rs->get('leagueID');
            if ($leagueID==$currentLeagueID)   $dataspace['selected_show'] =  'selected';

            //print_r($dataspace);
            $list_updated[$leagueID] = $dataspace;
        }


        $list_source_updated =& new ArrayDataSet($list_updated);

        return $list_source_updated;
     }

	 // get One league
     function getFirstLeagueID() {
        $site_id = Site::getSiteID();
    	return DBC::getOneValue("select min(L.leagueID) from sport_stat_leagues L where L.enabled=1
    	and  L.site_id = ".DBC::makeLiteral($site_id));
    }



}

?>
